home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: segmentation fault
- Date: 4 Apr 1996 17:19:15 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4k1sejINNc1v@keats.ugrad.cs.ubc.ca>
- References: <31636B02.55C6@cts.com> <4k0fjl$ir@ccshst05.cs.uoguelph.ca>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4k0fjl$ir@ccshst05.cs.uoguelph.ca>,
- Toby K Hay <thay@uoguelph.ca> wrote:
- >Frank Kragh (kragh@cts.com) wrote:
- >: When running a program, (which didn't yield any errors during compiling),
- >: I get a "Segmentation fault". Anybody know what this is and/or what
- >: might be wrong with my program?
- >
- > I posted this same question here recently and received plenty of
- >good advice which I'll summarize very briefly. A segmentation fault
- >while running a program usually (always?) means that your program is
- >accessing memory that wasn't allocated to it. It doesn't happen on a DOS
-
- Not quite. String literals _are_ allocated to the program, yet trying to write
- to them (on some systems) causes a segmentation fault.
-
- >machine becuase the OS doesn't do that check - the program can write
- >wherever it likes, maybe overwriting something vital and crashing the
- >machine, or maybe not. On UNIX the OS prevents it and halts the program
- >with a segmentation fault.
- > Most people who replied to my question suggested:
- >1) after a call to malloc() check that the returned pointer is not NULL
- >so that the program doesn't ever try to access memory via a null pointer.
- >2) check that all calls to free() had a valid pointer - no trying to
- >free() a pointer without a previous call to malloc() or that had already
- >been free()d.
- >3) check that the program wasn't reading or writing arrays beyond their
- >declared bounds (e.g. trying to access array elements RA[-1] or RA[7] on
- >an array declared as int RA[7] whose elements will be numbered 0 to 6).
-
- The latter is unlikely to cause a segmentation fault, because it is likely that
- there is valid data adjacent to the array. Protection is usually done to the
- granularity of a page, which can be anywhere from 512 bytes to several
- kilobytes. On some systems it's possible for a program to protect individual
- objects with fine granularity using segmentation, so that's a different story.
-
- >(This was the bug in my program.)
-
- You were fortunate that it was caught this way.
- --
-
-